import 'package:flutter/material.dart';
import 'package:wechat/chat/message.dart';
import 'package:wechat/contacts/contacts.dart';
import 'package:wechat/main.dart';
import 'package:wechat/personal/personal.dart';

import 'found/found.dart';

class AppPage extends StatefulWidget {
  const AppPage({super.key});

  @override
  State<AppPage> createState() => _AppPageState();
}

class _AppPageState extends State<AppPage> {
  int _currentIndex = 0;
  MessagePage? messagePage;
  Contacts? contacts;
  Found? found;
  Personal? personal;
  _currentPage() {
    switch (_currentIndex) {
      case 0:
        messagePage ??= const MessagePage();
        return messagePage;
      case 1:
        contacts ??= const Contacts();
        return contacts;
      case 2:
        found ??= const Found();
        return found;
      case 3:
        personal ??= const Personal();
        return personal;
    }
  }

  _popupMenuItem(String title, {String? imagePath, IconData? icon}) {
    return PopupMenuItem(
      child: Row(
        children: [
          imagePath != null
              ? Image.asset(
                  imagePath,
                  width: 32,
                  height: 32,
                )
              : SizedBox(
                  width: 32,
                  height: 32,
                  child: Icon(
                    icon,
                    color: Colors.white,
                  ),
                ),
          Container(
            padding: const EdgeInsets.only(left: 20),
            child: Text(
              title,
              style: const TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("微信"),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.of(context).pushNamed("/search");
            },
            child: const Icon(Icons.search),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30, right: 20),
            child: GestureDetector(
              onTap: () {
                showMenu(
                  context: context,
                  position: const RelativeRect.fromLTRB(500, 90, 20, 0),
                  items: <PopupMenuEntry>[
                    _popupMenuItem("发起圈聊",
                        imagePath: 'images/icon_menu_group.png'),
                    _popupMenuItem("添加朋友",
                        imagePath: 'images/icon_menu_addfriend.png'),
                    _popupMenuItem("扫一扫",
                        imagePath: 'images/icon_menu_scan.png'),
                    _popupMenuItem("收付款", icon: Icons.crop_free),
                    _popupMenuItem("帮助与反馈", icon: Icons.email),
                  ],
                );
              },
              child: const Icon(Icons.add),
            ),
          )
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        fixedColor: const Color(0xFF46c01b),
        selectedLabelStyle: const TextStyle(color: Color(0xFF46c01b)),
        unselectedLabelStyle: const TextStyle(color: Color(0xFF999999)),
        onTap: (index) {
          setState(() {
            _currentIndex = index;
            _currentPage();
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              _currentIndex == 0
                  ? 'images/weixin_pressed.png'
                  : 'images/weixin_normal.png',
              width: 32,
              height: 28,
            ),
            label: "微信",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              _currentIndex == 1
                  ? 'images/contact_list_pressed.png'
                  : 'images/contact_list_normal.png',
              width: 32,
              height: 28,
            ),
            label: "通讯录",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              _currentIndex == 2
                  ? 'images/find_pressed.png'
                  : 'images/find_normal.png',
              width: 32,
              height: 28,
            ),
            label: "发现",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              _currentIndex == 3
                  ? 'images/profile_pressed.png'
                  : 'images/profile_normal.png',
              width: 32,
              height: 28,
            ),
            label: "我",
          ),
        ],
      ),
      body: _currentPage(),
    );
  }
}
